package ab.mes.controller;

import ab.mes.entity.Staff;
import ab.mes.service.StaffService;
import ab.mes.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;

    //通过工号获取ID人员工号、姓名、照片
    @PostMapping("/getStaffByNumber")
    public Result getStaffByNumber(Integer number){
        Result result=new Result();
        try {
            result = staffService.getStaffByNumber(number);
        }catch (Exception e){
            result.setErrorMessage("查询人员失败！");
            e.printStackTrace();
        }
        return result;
    }

    //获取人员信息
    @PostMapping("/getStaff")
    public Result getStaff(){
        Result result;
        try {
            result = staffService.getStaff();
        }catch (Exception e){
            result = new Result();
            result.setErrorMessage("查询人员信息失败！");
            e.printStackTrace();
        }
        return result;
    }
    //新增人员信息
    @PostMapping("/insertStaff")
    public Result insertStaff(@RequestBody Staff staff){
        Result result=new Result();
        try {
            result = staffService.insertStaff(staff);
        }catch (Exception e){
            result.setErrorMessage("新增人员失败！");
            e.printStackTrace();
        }
        return result;
    }
    //根据id删除人员信息
    @PostMapping("/deleteStaff")
    public Result deleteStaff(Integer id_Of_Staff){
        Result result = new Result();
        try{
            result = staffService.deleteStaff(id_Of_Staff);
        }catch (Exception e){
            result.setErrorMessage("删除人员失败！");
            e.printStackTrace();
        }
        return result;
    }
    //修改人员信息
    @PostMapping("/editStaff")
    public Result editStaff(@RequestBody Staff staff){
        Result result = new Result();
        try{
            result = staffService.editStaff(staff);
        }catch (Exception e){
            result.setErrorMessage("修改人员失败！");
            e.printStackTrace();
        }
        return result;
    }
}
